#include <iostream>
#include <iomanip>
#include "EquationSolver.h"

using namespace std;

//B题各函数
double func1(double x)
{
        return 1/x - tan(x);
}

double func2(double x)
{
        return 1/x - pow(2,x);
}

double func3(double x)
{
        return pow(2,-x) + exp(x) + 2*cos(x) -6 ;
}

double func4(double x)
{
        return (x*x*x + 4*x*x + 3*x + 5)/(2*x*x*x - 9*x*x + 18*x - 2);
}

int main()
{
    typedef double (*fp)(double);
    fp fpi[] = {func1, func2, func3, func4};
    MyFunc f1(fpi[0]);
    MyFunc f2(fpi[1]);
    MyFunc f3(fpi[2]);
    MyFunc f4(fpi[3]);

        //特别注意：端点处无定义，应有特判，由单调性及正负性知值的区间
        //1/0返回inf, tan(pi/2)返回-1.63312e+16 , 故能运行
    double err = std::numeric_limits<double>::epsilon();
    Bisection B1(0.01,M_PI/2,err,err,100,f1);
    Bisection B2(0.01,1,err,err,100,f2);
    Bisection B3(1,3,err,err,100,f3);
    Bisection B4(0,4,err,err,100,f4);

    cout << "Question B: " << endl;
    cout << "The bisection result of f1 is " << setiosflags(ios::fixed) << setprecision(2) << B1.solve() << endl;
    cout << "The bisection result of f2 is " << setiosflags(ios::fixed) << setprecision(2) << B2.solve() << endl;
    cout << "The bisection result of f3 is " << setiosflags(ios::fixed) << setprecision(2) << B3.solve() << endl;
    double x = B4.solve();
    cout << "The bisection result of f4 is " << setiosflags(ios::fixed) << setprecision(5) << x << endl;
    cout << "But f4(x*) = " << f4(x) << " , f4(x) is not continue in [0,4]" << endl;
    cout << endl;

    return 0;
}